<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        slot="title"
        class="search-btn"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
      >搜索</van-button>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 文章频道列表 -->
    <van-tabs v-model="active" class="channel-tabs">
      <van-tab
        v-for="channel in channels"
        :title="channel.name"
        :key="channel.id"
        class="tab-item"
      >

        <ArticleList :channel="channel"/>
      </van-tab>
      <!-- 汉堡按钮定位把列表最后的位置给挡住了，解决办法就是在这里添加一个占位元素 -->
      <div slot="nav-right" class="wap-nav-palceholder"></div>

      <div slot="nav-right"
           @click="isChannelEditShow=true"
           class="wap-nav-warp"
      >
        <van-icon name="wap-nav" size="0.88rem" />
      </div>
    </van-tabs>


    <!-- /文章频道列表 -->
    <van-popup
      v-model="isChannelEditShow"
      position="bottom"
      class="channel-edit-popup"
      closeable
      close-icon-position="top-left"
      get-container="body"
      style="height: 100%"
    >
      <channel-edit
        :userChannels="channels"
        :active="active"
        @close="isChannelEditShow=false"
        @update-active="onUpdateActive"
      >

      </channel-edit>
    </van-popup>

  </div>
</template>

<script>
import {getUserChannels} from '@/api/user'
import  ArticleList from './components/article-list'
import ChannelEdit from './components/channel-edit'
import { mapState } from "vuex"
import {getItem} from '@/utils/storage'

export default {
  name: 'HomeIndex',
  components: {ArticleList,ChannelEdit},
  props: {},
  data () {
    return {
      active: 0, // 控制被激活的标签
      channels:[],
      isChannelEditShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.getUserChannels()
  },
  mounted () {},
  methods: {
    async getUserChannels(){
      try {
        let channels = []
        if (this.user) {
          // 已登录，请求获取线上的频道数据
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          // 未登录
          const localChannels = getItem('channels')
          if (localChannels) {
            // 有本地频道数据，则使用
            channels = localChannels
          } else {
            // 没有本地频道数据，则请求获取默认推荐的频道列表
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }

        // 将数据更新到组件中
        this.channels = channels
      } catch (err) {
        console.log(err)
        this.$toast('数据获取失败')
      }

    },
    onUpdateActive(index)
    {
      this.active=index;
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  /deep/ .van-nav-bar__title {
    max-width: none;
  }
  .search-btn {
    width: 277px;
    height: 32px;
    background: #5babfb;
    border: none;
    .van-icon {
      font-size: 16px;
    }
    .van-button__text {
      font-size: 14px;
    }
  }

  .channel-tabs {
    /deep/ .van-tab {
      border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
    }
    /deep/ .van-tabs__line {
      bottom: 20px;
      width: 15px !important;
      height: 3px;
      background: #3296fa;
    }
    .wap-nav-warp
    {
      position:fixed;
      right: 0%;
      background-color: #fff;
      opacity: .9;
      width: 33px;
      /deep/ .van-icon{
        font: 33px !important;
      }
    }
    .wap-nav-palceholder{
      width: 33px;
      flex-shrink: 0;
    }

  }
}
</style>
